package com.dayuanit.dy17.atm.util;

public class PageHelper<T> {
    //默认每页展示的个数
    public static final int default_page_size = 2;

    //总页数
    private int totalPage;
    //每页展示的数据
    private T pageData;

    //总条数
    private int totalCount;
    //当前页
    private int currentPage;
    //每页展示的条数
    private int pageSize;

    //分页语句的偏移量
    private int offset;

    private PageHelper(int totalCount, int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    private PageHelper() {

    }

    public static PageHelper init(int totalCount, int currentPage, int pageSize) {
        PageHelper pageHelper = new PageHelper(totalCount, currentPage, pageSize);
        pageHelper.initTotalPage();
        pageHelper.initOffset();
        return pageHelper;
    }

    public static PageHelper init(int totalCount, int currentPage) {
        return init(totalCount, currentPage, default_page_size);
    }

    private void initOffset() {
        offset = (currentPage - 1) * pageSize;
    }

    private void initTotalPage() {
        totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :  totalCount / pageSize + 1;
    }

    public int offset() {
        return offset;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public T getPageData() {
        return pageData;
    }

    public void setPageData(T pageData) {
        this.pageData = pageData;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }
}
